﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class SolMagic01
{
    //需要调整的最小人数
    //多退少补
    //记录多出来的或者少的
    //把缺少的补充到min，不一定符合要求
    public int MinAdjust(int[] groups, int minSize, int maxSize)
    {
        int adjustNum = 0;
        int restStudent = 0;
        Array.Sort(groups);//排序，小的是需要的，大的是多余的

        int left = 0;
        int right = groups.Length-1;

        while (left < right) { 
        
        int low= maxSize - groups[left];
        int high=groups[right]-maxSize;
            if (low > high) { //补充不够
                groups[left] += high;//调整
                adjustNum += high;
                right--;


            }
            else//补充不完
            {
                groups[right] -= low;//调整
                adjustNum += low;
                left++;
            }     
        
        }
        return adjustNum;
    }
    public class SolutionMagic012
    {
        public int AdjustCourseSelection(int[] groups, int minSize, int maxSize)
        {
            int adjustments = 0;
            int n = groups.Length;

            // 找出选课人数不在范围内的课程
            List<int> overSubscribed = new List<int>();
            List<int> underSubscribed = new List<int>();

            for (int i = 0; i < n; i++)
            {
                if (groups[i] > maxSize)
                {
                    overSubscribed.Add(i);
                }
                else if (groups[i] < minSize)
                {
                    underSubscribed.Add(i);
                }
            }

            // 调整选课人数
            foreach (int over in overSubscribed)
            {
                while (groups[over] > maxSize && underSubscribed.Count > 0)
                {
                    int under = underSubscribed[0];
                    int moveCount = Math.Min(groups[over] - maxSize, minSize - groups[under]);//为什么要找最小值

                    groups[over] -= moveCount;
                    groups[under] += moveCount;
                    adjustments += moveCount;

                    if (groups[under] >= minSize)
                    {
                        underSubscribed.RemoveAt(0);
                    }
                }
            }

            return adjustments;
        }
    }
}

class ProMagic01
{
    static void MainMagic01(string[] args)
    {
        string s;

        while ((s = Console.ReadLine()) != null)
        {

            string[] tokens = s.Split(',');
            int[] nums = new int[tokens.Length];
            for (int i = 0; i < nums.Length; i++)
            {

                nums[i] = int.Parse(tokens[i]);
            }
            s = Console.ReadLine();
            int min= int.Parse(s);
            s = Console.ReadLine();
            int max= int.Parse(s);

            SolMagic01 sol= new SolMagic01();
          int res=  sol.MinAdjust(nums, min, max);
            Console.WriteLine(res);

        }
    }
}